﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using QunYing.BLL;

namespace Client
{
    public partial class FrmNoticeManager : BaseChildForm
    {
        public static FrmNoticeManager frm = null;
        public FrmNoticeManager()
        {
            InitializeComponent();
            this.sharpGrid1.AddPopupMenu(new ToolStripSeparator(), null);
            this.sharpGrid1.AddPopupMenu(new ToolStripMenuItem("删除选择公告"), RemoveNotice);
            frm = this;
            this.dateSpanOrder.StartDateEdit.EditValue = DateTime.Now.Date.AddMonths(-3);
            this.dateSpanOrder.EndDateEdit.EditValue = DateTime.Now.Date;
        }

        private void FrmNoticeManager_Load(object sender, EventArgs e)
        {
            QueryData();
        }
        public void QueryData()
        {
            QunYing.BLL.Notice notice = new Notice();
            DataTable dt = notice.GetList(GetFiter()).Tables[0];
            sharpGrid1.DataSource = dt;

            GridView view = this.sharpGrid1.MainView as GridView;
            view.Columns["公告ID"].Visible = false;
            view.Columns["显示顺序"].Visible = false;
        }
        private string GetFiter()
        {
            StringBuilder strFiter = new StringBuilder();
            strFiter.Append("1=1");
            if (this.dateSpanOrder.StartDateEdit.EditValue != null)
            {
                DateTime startDate = Convert.ToDateTime(this.dateSpanOrder.StartDateEdit.EditValue);
                strFiter.Append(" AND 发布时间>='" + startDate.ToString("yyyy-MM-dd 00:00:00")+"'");
            }
            if (this.dateSpanOrder.EndDateEdit.EditValue != null)
            {
                DateTime endDate = Convert.ToDateTime(this.dateSpanOrder.EndDateEdit.EditValue);
                strFiter.Append(" AND 发布时间<='" + endDate.ToString("yyyy-MM-dd 23:59:59")+"'");
            }
            strFiter.Append(" Order by 发布时间 DESC");
            return strFiter.ToString();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmPublishNotice frm = new FrmPublishNotice();
            frm.OPERTYPE = "ADD";
            frm.ShowDialog();
        }

        private void sharpGrid1_RowDoubleClickEvent(object sender, Spore.Controls.RowDoubleClickEventArg e)
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] selectedRows = view.GetSelectedRows();
            foreach (var v in selectedRows)
            {
                if (v >= 0)
                {
                    object obj = view.GetRow(v);
                    DataRowView row = obj as DataRowView;
                    if (row == null)
                        continue;
                    FrmPublishNotice frm = SingleObjManager.GetSingleObj<FrmPublishNotice>();
                    frm.OPERTYPE = "EDIT";
                    frm.Text = "修改公告";
                    frm.row = row;
                    frm.ShowDialog();
                    break;
                }
            }
        }

        private void RemoveNotice(object sender, EventArgs e)
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] selectedRows = view.GetSelectedRows();
            if (selectedRows.Length == 0)
            {
                GlobalInfo.MsgBoxShowError("请选择你要删除的公告");
                return;
            }
            else
            {
                if (XtraMessageBox.Show(GlobalInfo.UserLookAndFeel, "确认删除该公告吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
                    return;
                foreach (var v in selectedRows)
                {
                    object obj = view.GetRow(v);
                    DataRowView row = obj as DataRowView;
                    if (row == null)
                        continue;
                    QunYing.BLL.Notice notice = new Notice();
                    notice.Delete(Convert.ToInt32(row.Row["公告ID"]));
                }

                QueryData();
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            QueryData();
        }

        public override void BindData()
        {
            QueryData();
        }
    }
}